home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / LIB / MUI / PULLDOWN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  11.1 KB  |  470 lines

  1. /*
  2.  * Copyright (c) 1993-1997, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
  36.  */
  37.  
  38. #include <GL/glut.h>
  39. #include <mui/gizmo.h>
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <stdlib.h>
  43. #include <mui/uicolor.h>
  44. #include <mui/displaylist.h>
  45.  
  46. /*  M E N U   B A R   F U N C T I O N S  */
  47.  
  48. Pulldown *newpd(void)
  49. {
  50.     Pulldown *pd = (Pulldown *)malloc(sizeof(Pulldown));
  51.     pd->count = 0;
  52.     return pd;
  53. }
  54.  
  55. void muiAddPulldownEntry(muiObject *obj, char *title, int menu, int ishelp)
  56. {
  57.     Pulldown *pd = (Pulldown *)obj->object;
  58.     int space = obj->xmax - obj->xmin - 66;
  59.     int i, delta;
  60.  
  61.     if (ishelp) {
  62.     strcpy(pd->helpmenu.title, title);
  63.     pd->ishelp = 1;
  64.     pd->helpmenu.xoffset = obj->xmax - 58;
  65.     pd->helpmenu.menu = menu;
  66.     return;
  67.     }
  68.     if (pd->count == 29) muiError("muiAddPulldownEntry: more than 29 entries");
  69.     strcpy(pd->menus[pd->count].title, title);
  70.     pd->menus[pd->count].menu = menu;
  71.     pd->count++;
  72.     /* now recalculate spacings */
  73.     if (space > 50*pd->count) {
  74.     for (i = 0; i <= pd->count; i++)
  75.         pd->menus[i].xoffset = 8 + i*50;
  76.     } else {
  77.     delta = space/pd->count;
  78.     for (i = 0; i <= pd->count; i++)
  79.         pd->menus[i].xoffset = 8 + i*delta;
  80.     }
  81. }
  82.  
  83. int activemenu = -1;
  84. extern int menuinuse;
  85.  
  86. /* ARGSUSED2 */
  87. enum muiReturnValue  pdhandler(muiObject *obj, int event, int value, int x, int y)
  88. {
  89.     int i;
  90.     Pulldown *pd = (Pulldown *)obj->object;
  91.  
  92.     if( !muiGetEnable(obj) || !muiGetVisible(obj) ) return MUI_NO_ACTION;
  93.     
  94.     if (event == MUI_DEVICE_UP) {
  95.     for (i = 0; i < pd->count; i++)
  96.         if (pd->menus[i].xoffset-8 < x && x < pd->menus[i+1].xoffset-8) {
  97.         if (activemenu != pd->menus[i].menu && !menuinuse) {
  98.             glutSetMenu(activemenu = pd->menus[i].menu);
  99.             glutAttachMenu(GLUT_LEFT_BUTTON);
  100.         }
  101.         return MUI_NO_ACTION;
  102.         }
  103.     if (pd->ishelp && (x > pd->helpmenu.xoffset-8)) {
  104.         if ((activemenu != pd->helpmenu.menu) && !menuinuse) {
  105.         glutSetMenu(activemenu = pd->helpmenu.menu);
  106.         glutAttachMenu(GLUT_LEFT_BUTTON);
  107.         }
  108.         return MUI_NO_ACTION;
  109.     }
  110.     if (activemenu && !menuinuse) {
  111.         glutDetachMenu(GLUT_LEFT_BUTTON);
  112.         activemenu = -1;
  113.     }
  114.     }
  115.     return MUI_NO_ACTION;
  116. }
  117.  
  118. void    drawpulldown(muiObject *obj)
  119. {
  120.     int        i;
  121.     int        xmin, xmax, ymin, ymax;
  122.  
  123.     if (!muiGetVisible(obj))
  124.     return;
  125.  
  126.     drawsetup();
  127.  
  128.     xmin = obj->xmin;
  129.     ymin = obj->ymin;
  130.     xmax = obj->xmax;
  131.     ymax = obj->ymax;
  132.  
  133.     drawedges(xmin++,xmax--,ymin++,ymax--,uiDkGray,uiVyDkGray);
  134.     drawedges(xmin++,xmax--,ymin++,ymax--,uiWhite,uiDkGray);
  135.     drawedges(xmin++,xmax--,ymin++,ymax--,uiVyLtGray,uiMmGray);
  136.     drawedges(xmin++,xmax--,ymin++,ymax--,uiVyLtGray,uiMmGray);
  137.  
  138.     uiLtGray();
  139.         uirectfi(xmin,ymin,xmax,ymax);
  140.  
  141.     if (obj->object) {
  142.         Pulldown *pd = (Pulldown *)obj->object;
  143.  
  144.         for (i = 0; i < pd->count; i++) {
  145.         if (muiGetEnable(obj)) uiBlack(); else uiDkGray();
  146.         uicmov2i(obj->xmin + pd->menus[i].xoffset, obj->ymin + 8);
  147.         uicharstr(pd->menus[i].title, UI_FONT_NORMAL);
  148.         }
  149.     if (pd->ishelp) {
  150.         if(muiGetEnable(obj)) uiBlack(); else uiDkGray();
  151.         uicmov2i(obj->xmin + pd->helpmenu.xoffset, obj->ymin + 8);
  152.         uicharstr(pd->helpmenu.title, UI_FONT_NORMAL);
  153.     }
  154.     }
  155.  
  156.     drawrestore();
  157. }
  158.  
  159. #ifdef NOTDEF
  160.  
  161. /* static variables */
  162. static int offrightside;
  163. static PullDown *basepd;
  164. static int menubut;
  165. static int menuretval;
  166. static int inpdmode = 0;
  167. static int indopd = 0;
  168. static int savep;
  169. static short savedev[SAVELEN];
  170. static short saveval[SAVELEN];
  171.  
  172. extern    MenuBar    *locatedmb;
  173.  
  174. static short pdinit = 0;
  175.  
  176. void    movemenubar(muiObject *obj)
  177. {
  178.     int    i, tx = MENUXENDGAP-9, totaltwidth = 0;
  179.     int    xsize, ysize, width;
  180.  
  181.     mb->xmin = mb->xorg;
  182.     mb->xmax = mb->xorg+xsize-1;
  183.     mb->ymin = mb->yorg+ysize-MENUBARHEIGHT;
  184.     mb->ymax = mb->yorg+ysize-1;
  185.  
  186.     width = mb->xmax - mb->xmin + 1;
  187.  
  188.     font(PULLDOWNFONT);    /* for correct strwidths */
  189.     for (i = 0; i < mb->count; i++)
  190.         totaltwidth += strwidth(mb->pds[i]->title) + 2*MENUXENDGAP;
  191.  
  192.     for (i = 0; i < mb->count; i++) {
  193.     if (mb->pds[i]->title)
  194.         mb->pds[i]->twidth = strwidth(mb->pds[i]->title);
  195.         if ((mb->pds[i]->title) && (!strcmp(mb->pds[i]->title,"Help")))
  196.            mb->pds[i]->txorg = mb->xmax-MENUXENDGAP-mb->pds[i]->twidth-MENUXGAP;
  197.         else
  198.            mb->pds[i]->txorg = mb->xmin + tx;
  199.         mb->pds[i]->xorg = mb->pds[i]->txorg;
  200.     mb->pds[i]->yorg = mb->ymin-TITLESEP;
  201.     mb->pds[i]->orglocked = 1;
  202.     mb->pds[i]->mb = mb;
  203.  
  204.     if (totaltwidth > width)
  205.             tx += (width-2*MENUXENDGAP)/mb->count-2;
  206.         else
  207.             tx += mb->pds[i]->twidth+24;
  208.     } 
  209. }
  210.  
  211. MenuBar *newmenubar(void)
  212. {
  213.     MenuBar    *mb;
  214.  
  215.     mb = (MenuBar *)calloc(1,sizeof(MenuBar));
  216.     mb->count = 0;
  217.     mb->pds = 0;
  218.     mb->locate = -1;
  219.     mb->enable = 1;
  220.     mb->invisible = 0;
  221.  
  222.     return mb;
  223. }
  224.  
  225. /* ENABLE STATE FUNCTIONS FOR MENU BARS */
  226.  
  227. void    enablemb(MenuBar *mb)
  228. {
  229.     if (mb->enable)
  230.     return;
  231.     mb->enable = 1;
  232. }
  233.  
  234. void    disablemb(MenuBar *mb)
  235. {
  236.     if (!mb->enable)
  237.     return;
  238.     mb->enable = 0;
  239. }
  240.  
  241. short    getenablemb(MenuBar *mb)
  242. {
  243.     return mb->enable;
  244. }
  245.  
  246. /* LOCATE STATE FUNCTIONS FOR MENU BARS */
  247.  
  248. short    gethighlightmb(MenuBar *mb)
  249. {
  250.     return mb->locate;
  251. }
  252.  
  253. void    highlightmb(MenuBar *mb, int pdnum)
  254. {
  255.     short oldpdnum = mb->locate;
  256.  
  257.     if (mb->locate == pdnum)
  258.     return;
  259.     mb->locate = pdnum;
  260.     locatedmb = mb;
  261.     if (!mb->invisible) {
  262.     if (pdnum != -1)
  263.         drawmenubartext(mb,pdnum);
  264.     if (oldpdnum != -1)
  265.         drawmenubartext(mb,oldpdnum);
  266.     }
  267. }
  268.  
  269. void    unhighlightmb(MenuBar *mb)
  270. {
  271.     short oldpdnum = mb->locate;
  272.  
  273.     if (mb->locate == -1)
  274.     return;
  275.     mb->locate = -1;
  276.     if (locatedmb == mb)
  277.     locatedmb = 0;
  278.     if (!mb->invisible)
  279.     if (oldpdnum != -1)
  280.         drawmenubartext(mb,oldpdnum);
  281. }
  282.  
  283. /* VISIBLE STATE FUNCTIONS */
  284.  
  285. void    makevisiblemb(MenuBar *mb)
  286. {
  287.     if (!mb->invisible)
  288.     return;
  289.     mb->invisible = 0;
  290. }
  291.  
  292. void    makeinvisiblemb(MenuBar *mb)
  293. {
  294.     if (mb->invisible)
  295.     return;
  296.     mb->invisible = 1;
  297. }
  298.  
  299. short   getvisiblemb(MenuBar *mb)
  300. {
  301.     return 1-mb->invisible;
  302. }
  303.  
  304. void    loadmenubar(MenuBar *mb, int menucount, PullDown **pdarray)
  305. {
  306.     int    i;
  307.  
  308.     drawsetup();
  309.  
  310.     mb->count = menucount;
  311.     mb->pds = (PullDown **)calloc(menucount,sizeof(PullDown));
  312.     mb->locate = -1;
  313.     mb->enable = 1;
  314.     mb->invisible = 0;
  315.  
  316.     for (i = 0; i < mb->count; i++){
  317.     mb->pds[i] = pdarray[i];
  318.     }
  319.     movemenubar(mb);
  320.  
  321.     drawrestore();
  322. }
  323.  
  324. void    addtomenubar(MenuBar *mb, PullDown *pd)
  325. {
  326.     PullDown     **pds;
  327.     int     i;
  328.  
  329.     mb->count++;
  330.     pds = (PullDown **)calloc(mb->count,sizeof(PullDown));
  331.  
  332.     for (i = 0; i < mb->count-1; i++){
  333.     pds[i] = mb->pds[i];
  334.     }
  335.     pds[i] = pd;
  336.  
  337.     free(mb->pds);
  338.     mb->pds = pds;
  339. }
  340.  
  341. void addtopd(PullDown *pd, MenuItem *mi)
  342. {
  343.     MenuItem *m, *tail;
  344.  
  345.     tail = pd->entries;
  346.  
  347.     if (tail) {
  348.         while (tail->next)
  349.             tail = tail->next;
  350.     tail->next = mi;
  351.     } else
  352.     tail = pd->entries = mi;
  353.  
  354.     m = pd->entries;
  355.  
  356.     while (m) {
  357.         pd->nentries++;
  358.         m->no = pd->nentries;
  359.         m = m->next;
  360.     }
  361.  
  362.     fixuppd(pd);
  363. }
  364.  
  365. void    removefrommenubar(MenuBar *mb, PullDown *pd)
  366. {
  367.     PullDown    **pds;
  368.     int        i, j;
  369.  
  370.     mb->count--;
  371.     pds = (PullDown **)calloc(mb->count,sizeof(PullDown));
  372.  
  373.     for (i = 0,j = 0; i <= mb->count; i++) {
  374.     if (mb->pds[i] != pd) {
  375.         pds[j] = mb->pds[i];
  376.         j++;
  377.     }
  378.     }
  379.  
  380.     free(mb->pds);
  381.     mb->pds = pds;
  382. }
  383.  
  384. void    movemenubar(MenuBar *mb)
  385. {
  386.     int    i, tx = MENUXENDGAP-9, totaltwidth = 0;
  387.     int    xsize, ysize, width;
  388.  
  389.     getorigin(&mb->xorg, &mb->yorg);
  390.     getsize(&xsize, &ysize);
  391.     mb->xmin = mb->xorg;
  392.     mb->xmax = mb->xorg+xsize-1;
  393.     mb->ymin = mb->yorg+ysize-MENUBARHEIGHT;
  394.     mb->ymax = mb->yorg+ysize-1;
  395.  
  396.     width = mb->xmax - mb->xmin + 1;
  397.  
  398.     font(PULLDOWNFONT);    /* for correct strwidths */
  399.     for (i = 0; i < mb->count; i++)
  400.         totaltwidth += strwidth(mb->pds[i]->title) + 2*MENUXENDGAP;
  401.  
  402.     for (i = 0; i < mb->count; i++) {
  403.     if (mb->pds[i]->title)
  404.         mb->pds[i]->twidth = strwidth(mb->pds[i]->title);
  405.         if ((mb->pds[i]->title) && (!strcmp(mb->pds[i]->title,"Help")))
  406.            mb->pds[i]->txorg = mb->xmax-MENUXENDGAP-mb->pds[i]->twidth-MENUXGAP;
  407.         else
  408.            mb->pds[i]->txorg = mb->xmin + tx;
  409.         mb->pds[i]->xorg = mb->pds[i]->txorg;
  410.     mb->pds[i]->yorg = mb->ymin-TITLESEP;
  411.     mb->pds[i]->orglocked = 1;
  412.     mb->pds[i]->mb = mb;
  413.  
  414.     if (totaltwidth > width)
  415.             tx += (width-2*MENUXENDGAP)/mb->count-2;
  416.         else
  417.             tx += mb->pds[i]->twidth+24;
  418.     } 
  419. }
  420.  
  421. int    inmenubar(MenuBar *mb, int mx, int my) /* Window coordinates    */
  422. {
  423.     int i;
  424.     
  425.     if (!getenablemb(mb))
  426.     return -1;
  427.  
  428.     if (getdrawmode() == NORMALDRAW) {
  429.     mx += mb->xorg;
  430.     my += mb->yorg;
  431.     }
  432.     if (mb->xmin <= mx && mx <= mb->xmax && 
  433.     mb->ymin+2 <= my && my <= mb->ymax-2)    /* +-2 for locate highlight*/
  434.        for (i = 0; i < mb->count; i++) {
  435.             if ((mx >= mb->pds[i]->txorg) &&
  436.                 (mx <= mb->pds[i]->txorg+mb->pds[i]->twidth+MENUXGAP*2))
  437.         return i;
  438.     }
  439.     return -1;
  440. }
  441.  
  442. short    locatemenubar(MenuBar *mb, int mx, int my) /* window coordinates */
  443. {
  444.     int highlight = gethighlightmb(mb);
  445.     int inmb  = inmenubar(mb,mx,my);
  446.  
  447.     if (!getenablemb(mb) || !getvisiblemb(mb) || (highlight == inmb))
  448.         return 0;
  449.     if (inmb != -1) {
  450.     unlocateall();
  451.         highlightmb(mb,inmb);
  452.     return 1;
  453.     } else {
  454.     unhighlightmb(mb);
  455.     return 0;
  456.     }
  457. }
  458.  
  459. /* DRAWING MENUBAR FUNCTIONS */
  460.  
  461. void    drawmenubarnow(MenuBar *mb)
  462. {
  463.     uifrontbuffer(1);
  464.     drawmenubar(mb);
  465.     uifrontbuffer(0);
  466. }
  467.  
  468.  
  469. #endif /* NOTDEF */
  470.